package edu.hm.compiler.scanner.token;

import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;

import edu.hm.compiler.I18NCompiler;

/**
 * @author Saruman
 */
public class TokenClassAdministrator implements ITokenClassAdministrator
{
    private final Map< String, ITokenClass > tokenClassTable = new HashMap<>();

    @Override
    public ITokenClass add( final String tokenName )
    {
        if( this.contains( tokenName ) )
            throw new IllegalArgumentException( I18NCompiler.getString( "ClassAlreadyExists", tokenName ) ); //$NON-NLS-1$ 
        return this.tokenClassTable.put( tokenName, new TokenClass( tokenName ) );
    }

    @Override
    public ITokenClass getByName( final String name )
    {
        final ITokenClass iTokenClass = this.tokenClassTable.get( name );
        if( iTokenClass == null )
            throw new NoSuchElementException(
                    I18NCompiler.getString( "ClassNotRegistered", name ) ); //$NON-NLS-1$ 
        return iTokenClass;
    }

    @Override
    public boolean contains( final String tokenName )
    {
        return this.tokenClassTable.containsKey( tokenName );
    }

}
